﻿using System;
using System.Windows.Controls;
using System.Windows;
using GrainManage.Tools;

namespace GrainManage.Base
{
    public class BasePage : Page
    {
        protected readonly IShowContent UI;

        public BasePage()
            : base()
        {
            UI = new UIHelper(Dispatcher);
        }
        //*****************************************静态方法与字段*****************************************

        protected static Window MainWindow;

        protected static Uri GetUri(string folder, string resource)
        {
            return new Uri(string.Format("pack://application:,,,/{0}/{1}", folder, resource));
        }

        //*****************************************实例方法*****************************************

        protected void TryBeginInvoke(Action tryAction, Action finallyAction, Action callBack)
        {
            SafeInvoke.TryBeginInvoke(tryAction, finallyAction, callBack, Dispatcher);
        }

        protected void TryBeginInvoke<T>(Func<T> func, Action<T> callBack)
        {
            SafeInvoke.TryBeginInvoke(func, null, callBack, Dispatcher);
        }

        protected void InvokeUI(Action uiAction)
        {
            SafeInvoke.InvokeUI(uiAction, Dispatcher);
        }

        protected void BeginInvokeUI(Action uiAction)
        {
            SafeInvoke.BeginInvokeUI(uiAction, Dispatcher);
        }

        protected void NavigateTo(string pageName, params object[] paramerers)
        {
            var uri = GetUri("Pages", pageName.EndsWith(".xaml") ? pageName : pageName + ".xaml");
            NavigationService.Navigate(uri, paramerers);
        }

    }
}
